﻿using MD5Hash;
using SmartHotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartHotel.DAL
{
    public class MemberDAL
    {
        public MemberDAL(HotelContext hotelContext) {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public int Add(Member member) { 
           
            HotelContext.Members.Add(member);
            return HotelContext.SaveChanges();
        }
        /// <summary>
        /// 根据用户名查询用户
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns>用户</returns>
        public Member GetMemberByName(string name) {
            return HotelContext.Members.Where(x=>x.Name==name).FirstOrDefault();
        }


        public Member GetMemberById(int id)
        {
            return HotelContext.Members.Where(x => x.Id == id).FirstOrDefault();
        }

        public int Update(Member member) {
            Member member1 = GetMemberById(member.Id);
            if (member1 != null) { //存在
                member1.Email = member.Email;
                member1.Level = member.Level;
                //member1.Name = member.Name;
                member1.Phone = member.Phone;
            }
            return HotelContext.SaveChanges();
        }
    }
}
